package com.sgb.goods.view.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityAddGoodsBrandBinding;
import com.sgb.goods.event.GoodsEvent;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.viewmodel.AddGoodsBrandModel;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.Objects;

/**
 * 描述：添加品牌（询价发布、新增采购品 ）
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.10
 */
@Route(path = Constants.ARouter.AR_ADD_GOODS_BRAND)
public class AddGoodsBrandActivity extends BaseActivity<GoodsActivityAddGoodsBrandBinding, AddGoodsBrandModel> {

    public static void start(Context context, String typeId) {
        Intent intent = new Intent();
        intent.putExtra("cid3", typeId);
        intent.setClass(context, AddBrandActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, AddBrandActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_add_goods_brand;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(AddGoodsBrandModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        initView();
    }

    @SuppressLint("ClickableViewAccessibility")
    protected void initView() {
        String cid3 = getIntent().getStringExtra("cid3");
        Objects.requireNonNull(mViewModel.brandEntity3.get()).setCid3(cid3);
        Objects.requireNonNull(mViewModel.brandEntity3.get()).setId("");
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.head.title.setText("添加品牌");
        mDataBinding.goodsSaving.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mDataBinding.itemTvName.getContent())) {
                MToast.showToast(AddGoodsBrandActivity.this, "请输入品牌名称");
                return;
            }
            Objects.requireNonNull(mViewModel.brandEntity3.get()).setMark(mDataBinding.etMark.getText().toString().trim());
            mViewModel.saveBrand();
        });
        mDataBinding.goodsScrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mDataBinding.goodsScrollView.setFocusable(true);
        mDataBinding.goodsScrollView.setFocusableInTouchMode(true);
        mDataBinding.goodsScrollView.setOnTouchListener((v, event) -> {
            v.requestFocusFromTouch();
            return false;
        });
    }

    @Override
    protected void initObserve() {
        mViewModel.onSuccess.observe(AddGoodsBrandActivity.this, aBoolean -> {
            if (aBoolean) {
                showSuccess();
            }
        });
    }

    public void showSuccess() {
        GoodsEvent goodsEvent = GoodsEvent.make(Constants.GoodsEvent.EVENT_BRAND_ADD);
        EventBus.getDefault().post(goodsEvent);
        finish();
    }

}

